﻿using Sc.Domain;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Persistence
{
    public class DriverCertificateDAO
    {
        #region  BasicMethod

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DriverCertificate model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into DriverCertificates(");
            strSql.Append("Driver,BaseItemId,CertificateCode,Score,StartTime,UsefulLife,State,CreateTime,UserNo,OrgzNo)");
            strSql.Append(" values (");
            strSql.Append("@Driver,@BaseItemId,@CertificateCode,@Score,@StartTime,@UsefulLife,@State,@CreateTime,@UserNo,@OrgzNo)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Driver", SqlDbType.Int,4),
					new SqlParameter("@BaseItemId", SqlDbType.Int,4),
					new SqlParameter("@CertificateCode", SqlDbType.NVarChar,50),
					new SqlParameter("@Score", SqlDbType.Decimal,5),
					new SqlParameter("@StartTime", SqlDbType.Date,3),
					new SqlParameter("@UsefulLife", SqlDbType.Int,4),
					new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@UserNo", SqlDbType.Int,4),
					new SqlParameter("@OrgzNo", SqlDbType.Int,4)};
            parameters[0].Value = model.Driver;
            parameters[1].Value = model.BaseItemId;
            parameters[2].Value = model.CertificateCode;
            parameters[3].Value = model.Score;
            parameters[4].Value = model.StartTime;
            parameters[5].Value = model.UsefulLife;
            parameters[6].Value = model.State;
            parameters[7].Value = model.CreateTime;
            parameters[8].Value = model.UserNo;
            parameters[9].Value = model.OrgzNo;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
            
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DriverCertificate model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update DriverCertificates set ");
            strSql.Append("Driver=@Driver,");
            strSql.Append("BaseItemId=@BaseItemId,");
            strSql.Append("CertificateCode=@CertificateCode,");
            strSql.Append("Score=@Score,");
            strSql.Append("StartTime=@StartTime,");
            strSql.Append("UsefulLife=@UsefulLife");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Driver", SqlDbType.Int,4),
					new SqlParameter("@BaseItemId", SqlDbType.Int,4),
					new SqlParameter("@CertificateCode", SqlDbType.NVarChar,50),
					new SqlParameter("@Score", SqlDbType.Decimal,5),
					new SqlParameter("@StartTime", SqlDbType.Date,3),
					new SqlParameter("@UsefulLife", SqlDbType.Int,4),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.Driver;
            parameters[1].Value = model.BaseItemId;
            parameters[2].Value = model.CertificateCode;
            parameters[3].Value = model.Score;
            parameters[4].Value = model.StartTime;
            parameters[5].Value = model.UsefulLife;
            parameters[6].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DriverCertificates ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)			};
            parameters[0].Value = Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from DriverCertificates ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DriverCertificate GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from Vw_DriverCertificates ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            DriverCertificate model = new DriverCertificate();
            DataTable ds = DbHelperSQL.Query(CommandType.Text, strSql.ToString(), parameters);
            if (ds.Rows.Count > 0)
            {
                return DataRowToModel(ds.Rows[0]);
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DriverCertificate DataRowToModel(DataRow row)
        {
            DriverCertificate model = new DriverCertificate();
            if (row != null)
            {
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (row["Driver"] != null && row["Driver"].ToString() != "")
                {
                    model.Driver = int.Parse(row["Driver"].ToString());
                    model.Name = row["Name"].ToString();
                }
                if (row["BaseItemId"] != null && row["BaseItemId"].ToString() != "")
                {
                    model.BaseItemId = int.Parse(row["BaseItemId"].ToString());
                    model.BaseItemName = row["BaseItemName"].ToString();
                }
                if (row["CertificateCode"] != null)
                {
                    model.CertificateCode = row["CertificateCode"].ToString();
                }
                if (row["Score"] != null && row["Score"].ToString() != "")
                {
                    model.Score = decimal.Parse(row["Score"].ToString());
                }
                if (row["StartTime"] != null && row["StartTime"].ToString() != "")
                {
                    model.StartTime = DateTime.Parse(row["StartTime"].ToString());
                }
                if (row["UsefulLife"] != null && row["UsefulLife"].ToString() != "")
                {
                    model.UsefulLife = int.Parse(row["UsefulLife"].ToString());
                }
                if (row["State"] != null && row["State"].ToString() != "")
                {
                    model.State = int.Parse(row["State"].ToString());
                }
                if (row["CreateTime"] != null && row["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
                }
                if (row["UserNo"] != null && row["UserNo"].ToString() != "")
                {
                    model.UserNo = int.Parse(row["UserNo"].ToString());
                }
                if (row["OrgzNo"] != null && row["OrgzNo"].ToString() != "")
                {
                    model.OrgzNo = int.Parse(row["OrgzNo"].ToString());
                }
            }
            return model;
        }
        #endregion  BasicMethod
    }
}
